*** This code is used to produce figures 5 & 6 and Tables A.15 & A.16								***
*** For Tuttnauer & Wegmann in APSR 																***
*** "Voting for Votes: Opposition Parties’ Legislative Activity and Electoral Outcomes"				***

** Install needed packages
ssc install coefplot, replace /* Generate coefficient plot graphs */
ssc install outreg2, replace /* Export regression tables to Word */

use "TW_survey_data.dta", clear 

** General description of the data 
tab party_unique
tab respondent_unique
	
*** Generating Figure 5 and Table A.15

* Model 1 - additive sympathy model
reg like_party conflict_rate perceived_dist past_vote_for expert_ext vote_share i.country_id i.party_id c.age##c.age ///
	male i.education, cluster(id)
outreg2 using table_a15.doc, replace dec(3) ctitle(Likability)
estimate store like_additive

* Model 2 - additive extremism model 
reg perceived_ext c.conflict_rate perceived_dist past_vote_for expert_ext vote_share ideo_gap i.country_id i.party_id ///
	c.age##c.age male i.education, cluster(id)
outreg2 using table_a15.doc, append dec(3) ctitle(Extremism)
estimate store extreme_additive

* Model 3 - interactive sympathy model (specifically run for use in coefplot) 
reg like_party conflict_rate vote_share confXvote perceived_dist past_vote_for expert_ext i.country_id i.party_id ///
	c.age##c.age male i.education, cluster(id)
estimate store like_interaction

* Model 4 - interactive extremism model (specifically run for use in coefplot)
reg perceived_ext conflict_rate ideo_gap confXideo perceived_dist past_vote_for expert_ext vote_share i.country_id i.party_id ///
	c.age##c.age male i.education, cluster(id)
estimate store extreme_interaction

* Generating Figure 5
coefplot like_additive like_interaction, bylabel("Likability") || ///
	extreme_additive extreme_interaction, bylabel("Extremism") || ///
		, keep(conflict_rate perceived_dist past_vote_for_party expert_extreme ideo_gap vote_share ///
				confXvote confXideo) ///
	scheme(plotplain) xline(0, lpattern(solid)) ///
	coeflabels(conflict_rate = "Conflict" perceived_dist = "Perceived distance" ///
				past_vote_for_party = "Voted for party" expert_extreme = "Extremism (experts)" ///
				ideo_gap = "Ideo. distance" confXideo = "Conflict x Ideo. distance" vote_share = "Vote share (t-1)" ///
				confXvote = "Conflict x Vote share")  ///
	ciopt(recast(rcap)) legend(off) byopts(xrescale)
graph export fig5.tif, replace

* Model 3 - interactive sympathy model (re-run for use in Figure 6 and outreg2)
reg like_party c.conflict_rate##c.vote_share perceived_dist past_vote_for expert_ext i.country_id i.party_id ///
	c.age##c.age male i.education, cluster(id)
outreg2 using table_a15.doc, append dec(3) ctitle(Likability)
margins, dydx(conflict_rate) at(vote_share = (0 (.1) .4))
marginsplot, scheme(plotplain) name(int1, replace) ///
	ytitle("") ///
	xtitle("vote share") ///
	title("DV = Likability") ///
	addplot(hist vote_share, yaxis(2) yscale(axis(2) alt) ///
	fintensity(inten30) color(%30) legend(off) ytitle("Density of vote share", axis(2) size(small)))
	
* Model 4 - interactive extremism model (re-run for use in Figure 6 and outreg2)
reg perceived_ext c.conflict_rate##c.ideo_gap perceived_dist past_vote_for expert_ext vote_share i.country_id i.party_id ///
	c.age##c.age male i.education, cluster(id)
outreg2 using table_a15.doc, append dec(3) ctitle(Extremism)
margins, dydx(conflict_rate) at(ideo_gap = (0 (.1) .7))
marginsplot, scheme(plotplain) name(int2, replace) ///
	ytitle("Effect of conflict on respondent perception") ///
	xtitle("Ideological distance") ///
	title("DV = Perceived extremism") ///
	addplot(hist ideo_gap, yaxis(2) yscale(axis(2) alt) ///
	fintensity(inten30) color(%30) legend(off) ytitle("Density of ideological distance", ///
	axis(2) size(small)) xlabel(0 (.1) .7))	

graph combine int1 int2, col(2)
graph export fig6.tif, replace

* Multilevel models (robustness check)
mixed like_party c.conflict_rate##c.vote_share perceived_dist past_vote_for expert_ext i.country_id i.party_id, || id:
outreg2 using table_a15.doc, append dec(3) ctitle(2 Levels - Likability)

mixed perceived_ext c.conflict_rate##c.ideo_gap vote_share perceived_dist past_vote_for expert_ext i.country_id i.party_id ///
	, || id:
outreg2 using table_a15.doc, append dec(3) ctitle(2 Levels - Extremism)

mixed like_party c.conflict_rate##c.vote_share perceived_dist past_vote_for expert_ext  ///
c.age##c.age male i.education, || country_id: || party_id:
outreg2 using table_a15.doc, append dec(3) ctitle(3 Levels - Likability)

mixed perceived_ext c.conflict_rate##c.ideo_gap vote_share perceived_dist past_vote_for expert_ext  ///
	c.age##c.age male i.education, || country_id: || party_id:
outreg2 using table_a15.doc, append dec(3) ctitle(3 Levels - Extremism)

** Generating Table A.16

reg perceived_ideo conflict_rate ideo_gap perceived_dist past_vote_for expert_ext vote_share ///
	i.country_id i.party_id ///
	c.age##c.age male i.education, cluster(id)
outreg2 using table_a16, word replace dec(3)
reg perceived_ideo c.conflict_rate##c.ideo_gap perceived_dist past_vote_for expert_ext vote_share ///
	i.country_id i.party_id ///
	c.age##c.age male i.education, cluster(id)
outreg2 using table_a16, word append dec(3)
